package cn.echo.modules.sys.controller.admincontroller;


import cn.echo.modules.cms.utils.StringUtf8;
import cn.echo.modules.sys.dao.SysAdminMapper;
import cn.echo.modules.sys.entity.SysAdmin;
import cn.echo.modules.sys.service.SiteService;
import cn.echo.modules.sys.service.SysAdminService;
import cn.echo.modules.sys.utils.MyBatisUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Date;
import java.util.UUID;

/**
 * @author 张翼飞
 *
 * Created by Administrator on 2017/4/19.
 */
@Controller
@RequestMapping("admin")
public class AdminC {
    @Autowired
    private SysAdminMapper sysAdminMapper;//注入mapper对象
    @Autowired
    private SysAdminService sysAdminService;
    @RequestMapping(value="add",method = RequestMethod.POST)
    public String add(String names, String loginNames, String phone, String adminNum,
                      String password, Model model){

        SqlSession session  = null;
        //openSesson参数默认为false设置为true后事务会自动提交，增加，修改，删除才可以成功
        session = MyBatisUtils.getSqlFactory().openSession(true);
        SysAdminMapper sysAdminMapper = session.getMapper(SysAdminMapper.class);

        SysAdmin sysAdmin = new SysAdmin();
        sysAdmin.setId(UUID.randomUUID().toString());
        sysAdmin.setName(names);
        sysAdmin.setIdNumber(adminNum);
        sysAdmin.setLoginName(loginNames);
        sysAdmin.setPassword(password);
        sysAdmin.setPhone(phone);
        sysAdmin.setCreateDate(new Date());
        if( sysAdminMapper.insertAdmin(sysAdmin)) {


            model.addAttribute("message", "创建用户成功");

        }

        return "redirect:/admin/SysAdmin?pageNum=1&pageSize=5";
    }
    @RequestMapping(value="SysAdmin",method = RequestMethod.GET)
    public String page(Integer pageNum,Integer pageSize,Model model){


        //赋值页面页码大小自动帮我们计算，我们只需要在页面上使用PageInfo的数据就行
        try {
        PageHelper.startPage(pageNum,pageSize);

        }catch (Exception e){
            PageHelper.startPage(1,5);
        }


        Page<SysAdmin> adminList = (Page) sysAdminService.findAll(new SysAdmin());
        System.out.println(adminList);

        //分页信息
        PageInfo<SysAdmin> pageInfo = new PageInfo(adminList);
        model.addAttribute("page",pageInfo);


        return "sys/Admin/AdminPager";

    }
    @RequestMapping(value="delAdmin",method = RequestMethod.GET)
    public String delAdmin(String id,Model model){

//        SqlSession session  = null;
//        //openSesson参数默认为false设置为true后事务会自动提交，增加，修改，删除才可以成功
//        session = MyBatisUtils.getSqlFactory().openSession(true);
//        SysAdminMapper sysAdminMapper = session.getMapper(SysAdminMapper.class);
        SysAdmin sysAdmin = new SysAdmin();
        sysAdmin.setId(id);

        sysAdmin.setCreateDate(new Date());
        if( sysAdminMapper.delAdmin(sysAdmin)){


            model.addAttribute("message","删除用户成功");



        }



        return "redirect:/admin/SysAdmin?pageNum=1&pageSize=5";
    }


    @RequestMapping(value="UpdateAdmin",method = RequestMethod.POST)
    public String updateAdmin(String id,String names, String loginNames, String phone, String adminNum, String password, Model model){

        SysAdmin sysAdmin = new SysAdmin();
        sysAdmin.setId(id);
        sysAdmin.setName(names);
        sysAdmin.setIdNumber(adminNum);
        sysAdmin.setLoginName(loginNames);
        sysAdmin.setPassword(password);
        sysAdmin.setPhone(phone);
        sysAdmin.setUpdateDate(new Date());
        if( sysAdminMapper.updateAdmin(sysAdmin)) {


            model.addAttribute("message", "修改用户"+names+"成功");



        }

        return "redirect:/admin/SysAdmin?pageNum=1&pageSize=5";
    }

    @RequestMapping(value="UpdateAdmin",method = RequestMethod.GET)
    public String updateAdmin( String id, Model model){

        SysAdmin sysAdmin = sysAdminService.findById(id);



        model.addAttribute("adminUp", sysAdmin);

        return "sys/Admin/updateAdmin";
    }
    @RequestMapping(value="/addAdmin",method = RequestMethod.GET)
    public String updateAdmin(Model model){
        System.out.println("return \"sys/Admin/createAdmin\";");
        return "sys/Admin/createAdmin";
    }
}
